﻿using Core.Tools.IOC;
using Hardware.Reactor.Base;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using UI.Application.Initialize.Models;
using UI.Application.Share.Config;
using UI.Application.Share.Event;
using UI.Application.Share.Initialize;

namespace UI.Application.Initialize.Services
{
   
    /// <summary>
    /// 硬件生命周期管理器
    /// </summary>
    [ExposedService(Lifttime.Singleton,typeof(IHardwareManager))]
    public class HardwareManager: IHardwareManager
    {
        private ITemp1Reader Temp1Reader { get; }
        private ITemp2Reader Temp2Reader { get; }

        /// <summary>
        /// 表示硬件初始化完成
        /// </summary>
        public bool Initialized { get; set; }
        public ISystemConfigProvider SystemConfigProvider { get; private set; }


        public HardwareManager(
            ISystemConfigProvider systemConfigProvider,
            ITemp1Reader temp1Reader, 
            ITemp2Reader temp2Reader)
        {
            SystemConfigProvider = systemConfigProvider;
            Temp1Reader = temp1Reader;
            Temp2Reader = temp2Reader;

            PrismProvider.EventAggregator.GetEvent<ApplicationExitEvent>().Subscribe(Close);
        }

       
        /// <summary>
        /// 启动程序时，加载硬件
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<HardwareInitializeResult> InitAsync()
        {
            if (Initialized) throw new Exception("重复初始化硬件");

            //todo 将来去真正初始化硬件模块

            Task<bool> task_Temp1Reader = Task.Run(() => Temp1Reader.Init(SystemConfigProvider));
            Task<bool> task_Temp2Reader = Task.Run(() => Temp2Reader.Init(SystemConfigProvider));
            var list =  await Task.WhenAll(task_Temp1Reader, task_Temp2Reader);
            bool result = list.All(p => p);
            if (!result)
            {
                string msg = "硬件初始化失败:";
                if(!task_Temp1Reader.Result)
                {
                    msg += "1号反应釜温度模块|";
                }
                if (!task_Temp2Reader.Result)
                {
                    msg += "2号反应釜温度模块|";
                }

                return new HardwareInitializeResult
                {
                    Message = msg,
                    Success = false,
                };
            }            

            Initialized = true;

            PrismProvider.EventAggregator.GetEvent<InitializedSuccessEvent>().Publish();//硬件初始化完成的事件发布

            return new HardwareInitializeResult() {  Message = "硬件初始化完成", Success = true};

        }

        /// <summary>
        /// 关闭程序时，卸载硬件
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Close()
        {
            try
            {
                Temp1Reader.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            try
            {
                Temp2Reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
